import { message, Modal, notification } from 'ant-design-vue'
import moment from 'moment'
import * as Antd from 'ant-design-vue'
import { useRoute } from 'vue-router'
import { reactive, ref } from 'vue'
export let table_scroll_config = { x: 1200, y: false }
import { isJSON } from '@Untils'
import { getUserToken } from '@Config/server/localStorage'
//process.env.NODE_ENV development | TASK | Production
export let URL_PRIX =
  process.env.VUE_APP_MY_NODE_ENV == 'development'
    ? 'http://10.10.1.17:8090' //'https://service.xunmengvip.com:8080/xm-bulky-admin'
    : // ? 'http://test1.xunmengvip.com:7695'
    process.env.VUE_APP_MY_NODE_ENV == 'TASK'
    ? 'http://10.10.1.17:8090'
    : 'https://service.xunmengvip.com:8080/xm-bulky-admin'

export const wss = () =>
  URL_PRIX.replace('https://', 'wss://') + '/message' + '/' + getUserToken()

export let calcUrl =
  process.env.VUE_APP_MY_NODE_ENV == 'development'
    ? 'https://api.xunmengvip.cn/xm-bulky-purchaser/order/calcExpressPrice'
    : 'https://service.xunmengvip.com:8080/xm-bulky-purchaser/order/calcExpressPrice'

//记录每个页面上面的message对象，当页面跳转时，清除全部message
let _message = []
export let Messages = {
  add: (message) => {
    _message.push(message)
  },
  clear: () => {
    _message.map((x) => {
      typeof x == 'function'
        ? x()
        : x.notify
        ? notification.close(x.notify)
        : x.destroy
        ? x.destroy()
        : null
    })
    _message = []
  },
}
export const modalConfig = () => {
  return {
    width: 1400,
    destroyOnClose: true,
    //    class: 'noFooterButtonModal' paddingContextModal,
  }
}
/***
 *
 * @description 分页配置参数
 * @param
 * fn:Function :页面按钮切换时，回调函数 必填
 * _onChang:boolean 是否需要change函数  默认 true
 * _showSizeChange:boolean 是否需要showSizeChange函数  默认 true
 *callBack：Object {
onchangeCallBack:Function,_onChang函数执行的回调
showSizeChangePre:Function ,_showSizeChange函数执行的回调
 }
 *
 */
export let createPagination = (
  fn,
  _onChange = true,
  _showSizeChange = true,
  callBack
) => {
  let _obj = reactive({
    current: 1,
    pageSize: 100,
    total: 0,
  })

  if (_onChange && fn && fn instanceof Function) {
    _obj.onChange = (pageNumber, pageSize) => {
      _obj.current = pageNumber
      _obj.pageSize = pageSize
      callBack?.onchangeCallBack
        ? callBack?.onchangeCallBack instanceof Function
          ? callBack.onchangeCallBack()
          : null
        : null
      fn()
    }
  }
  if (_showSizeChange && fn && fn instanceof Function) {
    _obj.showSizeChange = (pageNumber, pageSize) => {
      _obj.current = 1
      _obj.pageSize = pageSize
      callBack?.showSizeChangePre
        ? callBack?.showSizeChangePre instanceof Function
          ? callBack.showSizeChangePre()
          : null
        : null
      fn()
    }
  }

  return _obj

  // return {
  //   current: 1,
  //   pageSize: 10,
  //   total: 0,
  //   // pageSizeOptions: ['10', '20', '30'],
  //   // showSizeChanger: true,
  //   onChange: async (pageNumber, pageSize) => {
  //     // let _result = searchData({
  //     //   ...toRaw(formstate.data),
  //     //   pageSize: pageSize,
  //     //   pageIndex: pageNumber,
  //     // })
  //     // fetchDataBack(data)
  //   },
  // }
}

export const ModalSize = {
  full: '100%',
  large: 1400,
  middle: 800,
  small: 600,
}

export const confirm = (attr, needReject = false) => {
  let promise = new Promise((resolve, reject) => {
    let confirm = Modal.confirm({
      title: '确认操作',
      okText: '确定',
      cancelText: '取消',
      ...attr,
      onOk: () => {
        resolve(true)
      },
      onCancel: () => {
        if (needReject) {
          reject('reject')
        }
        //  reject()
      },
    })

    Messages.add(confirm)
  })
  return promise
}

export const simplePopup = (attr) => {
  let _value = ref(null)
  return new Promise((resolve, reject) => {
    let win = Modal.confirm({
      closable: true,
      title: '请输入内容',
      content: (
        <Antd.Row gutter={[20, 20]} justify="space-around" align="middle">
          <Antd.Col span={24}>
            <Antd.Textarea
              autoSize={{ minRows: 6 }}
              value={_value.value}
              placeholder={attr.placeholder}
              onChange={(e) => {
                _value.value = e.target.value
              }}
            />
          </Antd.Col>
          <Antd.Col span={24}>
            <Antd.Space>
              <Antd.Button
                type="primary"
                onClick={() => {
                  resolve(_value.value)
                  win.destroy()
                  win = null
                }}
              >
                确定
              </Antd.Button>
              <Antd.Button
                type="default"
                onClick={() => {
                  win.destroy()
                  win = null
                }}
              >
                取消
              </Antd.Button>
            </Antd.Space>
          </Antd.Col>
        </Antd.Row>
      ),
      class: ['noFooterButtonModal', 'paddingContextModal'],
      ...attr,
      width: ModalSize.small,
    })
    return win
  })
}

export const orderMaxPrice = 100
/**
 *
 * 场次时间配置
 */
export const workTime = {
  start: moment('20:00:00', 'HH:mm:ss'),
  end: moment('20:00:00', 'HH:mm:ss'),
}

//价格排序 null 0:从低到高 1:从高到低
export const sortList = () => {
  return [
    {
      label: '从低到高',
      value: 0,
    },
    {
      label: '从高到低',
      value: 1,
    },
  ]
}

/**
 * @description 返回当前场次
 *
 */
export const currentSessions = () => {
  let _start = moment()
    .subtract(1, 'day')
    .hour(20)
    .minute(0)
    .second(0)
  let _end = moment()
    .hour(20)
    .minute(0)
    .second(0)

  // console.log(
  //   _start.format('YYYY-MM-DD HH:mm:ss'),
  //   _end.format('YYYY-MM-DD HH:mm:ss'),

  //   _start.isBetween(_start, _end, null, '[]')
  // )

  return { start: _start, end: _end }
}

export const isInNowSessions = (_targetTime) => {
  let _bet = currentSessions()
  let _r = moment(_targetTime).isBetween(_bet.start, _bet.end, null, '[]')
  // console.log(_targetTime, _r)

  return _r
}
//上传图片配置参数
export const uploadParams = () => {
  return {
    action: URL_PRIX + '/lib/picUpLoad',
    headers: {
      usertokenxm: getUserToken(),
    },
    showUploadList: false,
    multiple: true,
    //directory: true,
  }
}
